#!/bin/bash
CUR_DIR=$(cd "$(dirname "$0")";pwd)

cd $CUR_DIR

source /etc/profile || exit -1
source ../cluster_IP.conf || exit -1

# 计算原始数据块数量
cal_data_num_in_data_chunks() {
    local org_id=0
    local data_num=0
    # rm data_num.txt
    for node in ${CEPH_IPs[@]}; do
        local count=$(
            ssh $node "
                docker exec ceph-demo-${org_id} zsh -c '
                    source ~/.zshrc; 
                    rados -p testbench ls | grep -v @@ | grep -o benchmark_data | wc -l
                ' 2>/dev/null
            "
        )
        # local data=$(
        #     ssh $node "
        #         docker exec ceph-demo-${org_id} zsh -c '
        #             source ~/.zshrc; 
        #             rados -p testbench ls | grep -v @@ | grep -v last_metadata
        #         ' 2>/dev/null
        #     "
        # )
        # echo "$data" >> data_num.txt
        echo "[Org $org_id] data obj num: $count"
        data_num=$(($data_num+$count))
        org_id=$(($org_id+1))
    done
    echo "Total data obj num: $data_num"
    return $data_num
}

cal_data_num_in_parity_chunks() {
    local org_id=0
    local parity_num=0
    # rm parity_num.txt
    for node in ${CEPH_IPs[@]}; do
        local count=$(
            ssh $node "
                docker exec ceph-demo-${org_id} zsh -c '
                    source ~/.zshrc; 
                    rados -p testbench ls | grep @@ | grep -o benchmark_data | wc -l
                ' 2>/dev/null
            "
        )
        # local data=$(
        #     ssh $node "
        #         docker exec ceph-demo-${org_id} zsh -c '
        #             source ~/.zshrc; 
        #             rados -p testbench ls | grep @@
        #         ' 2>/dev/null
        #     "
        # )
        # echo "$data" >> parity_num.txt

        echo "[Org $org_id] parity obj num: $count"
        parity_num=$(($parity_num+$count))
        org_id=$(($org_id+1))
    done
    echo "Total parity obj num: $parity_num"
    return $parity_num
}

cal_data_num_in_data_chunks
cal_data_num_in_parity_chunks


# if [ $data_num1 -eq $data_num2 ]; then
#     echo "Redundancy is valid"
# else
#     echo "Redundancy is invalid"
# fi